//
//  EmotionListView.m
//  微博2期
//
//  Created by J on 15/12/13.
//
//

#import "EmotionListView.h"
#import "EmotionPageView.h"
@interface EmotionListView()<UIScrollViewDelegate>
@property(nonatomic,weak)UIScrollView * scrollView;
@property(nonatomic,weak)UIPageControl * pageControl;


@end


@implementation EmotionListView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor purpleColor];
        //1.UIScrollView
        UIScrollView * scrollView = [[UIScrollView alloc]init];
        scrollView.pagingEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO;//让scrollView 不创建横方向view
        scrollView.showsVerticalScrollIndicator = NO;//让scrollView 不创建竖直方向view

        [self addSubview:scrollView];
        self.scrollView = scrollView;
        self.scrollView.delegate = self;
//        scrollView.backgroundColor = [UIColor orangeColor];
        //2.pageControl
        UIPageControl * pageControl = [[UIPageControl alloc]init];
        self.pageControl = pageControl;
        [self addSubview:pageControl];
//        pageControl.backgroundColor = [UIColor orangeColor];
        pageControl.userInteractionEnabled = NO;//禁用点击
        [pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKeyPath:@"pageImage"];
        [pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKeyPath:@"currentPageImage"];
        pageControl.hidesForSinglePage = YES;
    }
    
    return self;
}
- (void)setEmotions:(NSArray *)emotions{
    _emotions = emotions;
    
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    NSLog(@"%zd",_emotions.count);
    //根据emotions创建对应个数的表情
    
    //1.设置页数
    NSUInteger count = ceil(emotions.count/EmotionPageSize);
    self.pageControl.numberOfPages =  count>1?count:0;
    self.pageControl.hidesForSinglePage = YES;//当只有一页隐藏
//    self.pageControl.numberOfPages =  (emotions.count + EmotionPageSize - 1)/EmotionPageSize;

    //2.创建用来显示每一页表情的容器
//    NSUInteger count = self.pageControl.numberOfPages;//页数

    for (int i =0; i<count; i++) {
        
        EmotionPageView * pageView = [[EmotionPageView alloc]init];
//        pageView.backgroundColor = RamdomColor;
        //计算这一页的表情范围
        NSRange range ;
        
        range.location = i*EmotionPageSize;//i*20
        NSInteger left = emotions.count-range.location;//剩余的表情个数(可以截取的)
        range.length = left;//20个
        if (left>=20) {//意味着这一页足够20个
            range.length = EmotionPageSize;//20ge
        }
        
        //这一页的表情
        pageView.emotions = [emotions subarrayWithRange:range];//生成子数组
        [self.scrollView addSubview:pageView];
        
        
    }
//      NSLog(@"%@",self.scrollView.subviews);
    //1 页20个
    //总共55个
    //0页 20个
    //1页 20个
    //2页 15个
    [self setNeedsLayout];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    NSLog(@"%@",self.scrollView.subviews);
    
}



- (void)layoutSubviews{
    [super layoutSubviews];
    //1.pageControl
    self.pageControl.width = self.width;
    self.pageControl.height = 35;
    self.pageControl.x = 0;
    self.pageControl.y = self.height - self.pageControl.height;
    
    //2.scrollView
    
    self.scrollView.width = self.width;
    self.scrollView.height = self.pageControl.y;
    self.scrollView.x = self.scrollView.y = 0;
    
    //3.设置scrollView内部每个控件
    
    NSUInteger count = self.scrollView.subviews.count;

    for (int i=0; i<count; i++) {
        EmotionPageView * pageView = self.scrollView.subviews[i];
        pageView.width =  self.scrollView.width;
        pageView.x = pageView.width*i;
        pageView.y = 0;
        pageView.height = self.scrollView.height;
    }
    //4.设置scrollView的contentSize
    self.scrollView.contentSize = CGSizeMake(count * self.scrollView.width, 0);

}

#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    double pageNum = scrollView.contentOffset.x / scrollView.width;
    self.pageControl.currentPage = (int)(pageNum+0.5);
    
    
}

@end
